﻿using HlELock.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HlELock.Domain
{
    public class HlELockDbContext:DbContext
    {
        public DbSet<RoomType> RoomTypes { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<CheckIn> CheckIns { get; set; }
        public DbSet<CheckInRoom> CheckInRooms { get; set; }
        public DbSet<LocalUser> LocalUsers { get; set; }
        public DbSet<HotelInfo> HotelInfos { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<CardLog> CardLogs { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connectionString = "Data Source=data";
            optionsBuilder.UseSqlite(connectionString);
        }

        public int ExecuteSqlRaw(string sql,params object[] parameters)
            =>Database.ExecuteSqlRaw(sql,parameters);


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                // 获取所有的字符串属性
                var stringProperties = entityType.ClrType.GetProperties()
                    .Where(p => p.PropertyType == typeof(string));

                foreach (var property in stringProperties)
                {
                    // 设置默认的最大长度
                    modelBuilder.Entity(entityType.Name).Property(property.Name).HasMaxLength(64);
                }
            }
        }
    }
}
